package org.jeecg.modules.recruit.appuser.service.impl;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.api.dto.req.user.AuthDto;
import org.jeecg.common.util.BeanUtils;
import org.jeecg.modules.recruit.appuser.entity.MmAppUser;
import org.jeecg.modules.recruit.appuser.entity.MmAppUserAuth;
import org.jeecg.modules.recruit.appuser.mapper.MmAppUserAuthMapper;
import org.jeecg.modules.recruit.appuser.mapper.MmAppUserMapper;
import org.jeecg.modules.recruit.appuser.service.IMmAppUserAuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @Description: app用户认证表
 * @Author: jeecg-boot
 * @Date: 2020-08-23
 * @Version: V1.0
 */
@Service
public class MmAppUserAuthServiceImpl extends ServiceImpl<MmAppUserAuthMapper, MmAppUserAuth> implements IMmAppUserAuthService {

    @Autowired
    private MmAppUserAuthMapper mmAppUserAuthMapper;

    @Autowired
    private MmAppUserMapper mmAppUserMapper;

    @Override
    public List<MmAppUserAuth> selectByMainId(String mainId) {
        return mmAppUserAuthMapper.selectByMainId(mainId);
    }

    @Override
    public boolean auth(AuthDto authDto) {
        //判断用户是否存在
        MmAppUser mmAppUser = mmAppUserMapper.selectById(authDto.getUserId());
        Assert.notNull(mmAppUser, "用户不存在 实名认证失败!");
        if (Integer.valueOf(1).equals(mmAppUser.getIsAuth())) {
            return true;
        }
        //判断是否存在正在认证中的申请
        List<MmAppUserAuth> list = lambdaQuery().eq(MmAppUserAuth::getUserId, authDto.getUserId())
                .eq(MmAppUserAuth::getStatus, "0").list();

        if (!CollectionUtils.isEmpty(list)) {
            throw new IllegalArgumentException("认证申请中请不要重复提交");
        }
        MmAppUserAuth appUserAuth = BeanUtils.copyProperties(authDto, MmAppUserAuth.class);
        save(appUserAuth);
        return true;
    }
}
